﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace SetListManager.Helpers
{

    public static class HtmlExtensions
    {
        public static MvcHtmlString MenuItem(this HtmlHelper helper,
            string linkText, string actionName, string controllerName, string strExtraClass = "", string strOtherActionsToMatch = "")
        {
            string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
            string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];

            var builder = new TagBuilder("li");

            // Set "active" class if the current action matches the one for this menu
            if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase)
                && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase))
                builder.AddCssClass("active");

            // Also set "active" class if our controller is correct and action is one of our "strOtherActionsToMatch"
            if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase)
                && strOtherActionsToMatch.ToUpper().Contains(currentActionName.ToUpper()))
                builder.AddCssClass("active");

            // If they specified a class name, add it to the menu item
            if (strExtraClass.Length > 0) builder.AddCssClass(strExtraClass);
            builder.InnerHtml = helper.ActionLink(linkText, actionName, controllerName).ToHtmlString();
            return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
        }

    }
}
